package org.ihaibi.cmp.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.ihaibi.cmp.entity.MenuInfo;
import org.ihaibi.cmp.entity.PageData;
import org.ihaibi.cmp.util.HttpClientUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/cmp")
public class CmpController extends BaseController{

	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(CmpController.class);

	@RequestMapping("/showInfos")
	public @ResponseBody List<Object> showUserInfos() {
		List<Object> userInfos = new ArrayList<Object>();

		userInfos.add(HttpClientUtils.doGet(HttpClientUtils.ROOT_URL + "/channel_class/"));
		userInfos.add("xiaoxue");
		return userInfos;
	}


	/**
	 * http://localhost:8080/ihaibiCMP/cmp/get.htmls?domainUrl=http://106.14.57.55:8090/api/v1/menu_info?query=delflag:0,fatherId:0&sortby=menuDisplayOrder&order=asc
	 * 
	 * @param url
	 * @return
	 */
	@RequestMapping("/HttpGetList")
	public @ResponseBody Object  HttpGetList(String domainUrl, String query, String sortby, String order, String limit,
			String offset) {
		String reuslt =  HttpClientUtils.doGet(getQueryUrl(domainUrl, query, sortby, order, limit, offset));
		JSONArray  myJsonArray = JSONArray.parseArray(reuslt);
		return myJsonArray;
	}
	
	@RequestMapping("/HttpGetTables")
	public @ResponseBody Object  HttpGetTables(String domainUrl, String query, String sortby, String order, String limit,
			String offset) {
		String reuslt =  HttpClientUtils.doGet(getQueryUrl(domainUrl, query, sortby, order, limit, offset));
		JSONArray  myJsonArray = JSONArray.parseArray(reuslt);
		JSONObject  myJson =  new JSONObject();
		myJson.put("aaData", myJsonArray);
		return myJson;
	}
	
	
	/**
	 * http://localhost:8080/ihaibiCMP/cmp/get.htmls?url=http://106.14.57.55:8090/api/v1/menu_info?query=delflag:0,fatherId:0&sortby=menuDisplayOrder&order=asc
	 * 
	 * @param url
	 * @return
	 */
	@RequestMapping("/HttpGet")
	public @ResponseBody Object  HttpGet(String domainUrl, String query, String sortby, String order, String limit,
			String offset) {
		String reuslt =  HttpClientUtils.doGet(getQueryUrl(domainUrl, query, sortby, order, limit, offset));
		JSONObject  myJson = JSONObject.parseObject(reuslt);
		return myJson;
	}
	
	
	
	@RequestMapping("/doSave")
	public @ResponseBody Object  doSave(Model model) {
		PageData pd = new PageData();
		pd = this.getPageData();
		
		String reuslt =  HttpClientUtils.doPost(pd.getString("domainUrl"), model);
		return reuslt;
	}
	
	
	
	
	
	
	
	/**
	 * 通用查询方法
	 * @param url
	 * @param query
	 * @param sortby
	 * @param order
	 * @param limit
	 * @param offset
	 * @return
	 */
	private String getQueryUrl(String domainUrl, String query, String sortby, String order, String limit,String offset){
		StringBuffer queryParm = new StringBuffer();
		queryParm.append("?s=s");
		
		if (StringUtils.isNotBlank(query)) {

			queryParm.append("&query=" + query);
		}
		if (StringUtils.isNotBlank(sortby)) {

			queryParm.append("&sortby=" + sortby);
		}
		if (StringUtils.isNotBlank(order)) {

			queryParm.append("&order=" + order);
		}
		if (StringUtils.isNotBlank(limit)) {

			queryParm.append("&limit=" + limit);
		}

		if (StringUtils.isNotBlank(offset)) {

			queryParm.append("&offset=" + offset);
		}
		return domainUrl + queryParm.toString();
	}
	
	
	
	
	
	
	
	
	/**
	 * 菜单
	 * @return
	 */
	@RequestMapping("/getMenus")
	public @ResponseBody Object  getMenus(String domainUrl, String query, String sortby, String order, String limit,
			String offset) {
		List<MenuInfo> list = getMenuList(getQueryUrl(domainUrl, query, sortby, order, limit, offset));
		for(MenuInfo info : list){
			query = "delflag:0,fatherId:"+info.getId();
			List<MenuInfo> sublist = getMenuList(getQueryUrl(domainUrl, query, sortby, order, limit, offset));
			info.setSubMenu(sublist);
		}
		return list;
	}
	
	
	
	/**
	 * 获取一级菜单
	 * 
	 * @return
	 * @throws Exception
	 */
		private List<MenuInfo> getMenuList(String domainUrl)  {
	
		List<MenuInfo> list = HttpClientUtils.doGetForList(domainUrl, MenuInfo.class);
		return list;
	}

	
}
